package demo3;
import demo2.EmptyException;
public class MyQueue {
    static class Node {
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;
    public Node last;
    public int usedSize;

    //入队
    public void offer(int val) {
        Node newNode = new Node(val);
        if (head == null) {
            this.head = newNode;
            this.last = newNode;
            usedSize++;
            return;
        }
        this.last.next = newNode;
        this.last = newNode;
        usedSize++;
    }

    //出队
    public int poll() {
        if (this.head == null) {
            return -1;
        }
        int del = this.head.val;
        this.head = this.head.next;
        //说明只有一个节点
        if (this.head == null) {
            this.last = null;
        }
        usedSize--;
        return del;
    }

    //获得栈顶元素
    public int peek() {
        if (this.head == null) {
            throw new EmptyException("队列为空");
        }
        return this.head.val;
    }
    public int getUsedSize() {
        return usedSize;
    }
}
